(function(window) {
    var publicfunc = {};
    function hasxxPermission(pername) {
        console.log(pername);
        var perms = [];
        if (!pername || pername.length <= 1) {
            api.alert({msg:'非法pername'},function(ret){
                return;
            });
        }
        perms.push(pername);
        var rets = api.hasPermission({
            list:perms
        });
        return rets;
    }
    var reqPermission = function(rets) {
        var pernames = [];
        for(var i=0;i<rets.length;++i) {
            if(!rets[i].granted) {
                pernames.push(rets[i].name);
                api.toast({
                    msg:'需要'+rets[i].name+'权限'
                });
            }
        }
        api.requestPermission({
            list:pernames,
        }, function(ret){
            for (var j=0;j<ret.list.length;++j) {
                if(!ret.list[i].granted) {
                    api.alert({msg:'获取授权失败'},function(ret){
                        api.closeWidget();
                    });
                } else {
                    api.toast({
                        msg:'获取'+ret.list[i].name+'权限成功'
                    });
                }
            }
            api.alert({msg:"授权已成功。"},function(ret){});
        });
    }
    publicfunc.clearDataBase = function(path) {
        var fs = api.require('fs');
        fs.rmdir({
            path:path
        }, function(ret, err){
            if(ret.status) {
                api.alert({msg:"清除数据库成功。"},function(ret){
                });
            } else {
                api.alert({msg:err.code+err.msg},function(ret){
                });
            }
        });
    }
    publicfunc.hasxxPermission = hasxxPermission;
    publicfunc.reqPermission = reqPermission;

    window.publicfunc = publicfunc;
})(window);